#ifndef TIFFRAWOVERLAYBASE_H
#define TIFFRAWOVERLAYBASE_H


#include "RawOverlay.h"
#include "Image_Config.h"



class TIFFOverlay: public QObject, public RawOverlay
{
    Q_OBJECT

public:
    // Constructors/Destructors
    TIFFOverlay( const QString filename );
    virtual ~TIFFOverlay();
    ilong getImageHeight() const;
    ilong getImageWidth() const;
    UINT getBitsPerPixel() const;
    bool isVertical() const;
    bool load();
    QString photoInterpToString() const;
    void draw( QPainter *p  , const QRect rect , const  QPoint point , QWidget *target = 0);
    QRect d_boundingRect;

protected:
    ImageMatrix *imat;
    QTime chrono;
    int num_colors;

    void getMinMaxSampleValue( float& min, float& max ) const;
    void readMinMaxSampleValues( TIFF* pTIFF );


    QColor getRowColColor( long row, long col );


    // Vertical TIFF and shader data
    int16  d_minSampleValue;
    int16  d_maxSampleValue;

    TIFF*  tif;   ///  core
    // Data members to hold TIFF tags
    uint16 d_bitsPerSample;
    uint16 d_samplesPerPixel;  /// _samplespp
    char*  d_copyright;
    char*  d_datetime;
    char*  d_description;
    uint32 d_imageLength;
    uint32 d_imageWidth;
    uint16 d_photometricInterpretation;
    uint32 d_rowsPerStrip;
    int fDPI;
    bool portrait; /// landscape
    qreal faktor;
    QVector<QRgb> Ccolors;

    // Buffering class
    class TIFFCache
    {
    public:
        // Construction/Destruction
        TIFFCache( TIFF* pTIFF, int rowsPerStrip, UINT cacheSize = 32 );
        ~TIFFCache();

        // Data Access members
        BYTE* getScanlineData( long row );

    private:
        // Internal buffer class
        class TIFFStripBuffer
        {
        public:
            TIFFStripBuffer();
            ~TIFFStripBuffer();

            bool isRowInStrip( int row ) const;
            tdata_t getStripData();
            void setStripData( tdata_t pStrip );
            void setStripLocation( int startRow, int endRow );

        protected:
            tdata_t d_pStrip;
            int     d_startRow;
            int     d_endRow;
        };

        // Data members
        TIFFStripBuffer* d_pStrips;
        TIFF*            d_pTIFF;
        int              d_rowsPerStrip;
        UINT             d_cacheSize;
        int              d_stripSize;
        int              d_scanlineSize;
    };
    TIFFCache* d_pCache;

private slots:
    ///////////void compose( QRect rect , int pos );

};





inline void TIFFOverlay::getMinMaxSampleValue( float& min, float& max ) const
{
    min = static_cast<float>( d_minSampleValue );
    max = static_cast<float>( d_maxSampleValue );
}

inline ilong TIFFOverlay::getImageHeight() const
{
    return d_imageLength;
}

inline ilong TIFFOverlay::getImageWidth() const
{
    return d_imageWidth;
}

inline bool TIFFOverlay::TIFFCache::TIFFStripBuffer::isRowInStrip( int row ) const
{
    return ( row >= d_startRow && row <= d_endRow );
}

inline tdata_t TIFFOverlay::TIFFCache::TIFFStripBuffer::getStripData()
{
    return d_pStrip;
}

inline void  TIFFOverlay::TIFFCache::TIFFStripBuffer::setStripData( tdata_t pStrip )
{
    d_pStrip = pStrip;
}

inline void TIFFOverlay::TIFFCache::TIFFStripBuffer::setStripLocation( int startRow,
        int endRow )
{
    d_startRow = startRow;
    d_endRow = endRow;
}

inline UINT TIFFOverlay::getBitsPerPixel() const
{
    return ( d_bitsPerSample * d_samplesPerPixel );
}

inline bool TIFFOverlay::isVertical() const
{
    return ( d_minSampleValue != d_maxSampleValue );
}


















#endif